package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author gsy
 * @date 2021年08月30日17:57
 * Description
 */
@RestController
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    //查询商品详情信息
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId) {
        Map map = new HashMap();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1.查询sku基本信息和sku图片信息列表
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
            //查询sku价格信息  需要单独查询 实时更新 不能放入redis缓存中
            BigDecimal price = productFeignClient.getPrice(skuId);
            map.put("price", price);
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFuture2 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //2.获取分类信息
            BaseCategoryView category = productFeignClient.getCategory(skuInfo.getCategory3Id());
            map.put("categoryView", category);
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFuture3 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //3.查询销售属性及属性值集合(选中)
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getSpuId(), skuId);
            map.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);
        CompletableFuture<Void> completableFuture4 = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4.点击其他销售属性值的组合，跳转到另外的sku页面
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            map.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);
        //增加热度评分
        CompletableFuture.runAsync(()->{
            listFeignClient.incrHotScore(skuId,1);
        },threadPoolExecutor);
        CompletableFuture.allOf(skuInfoCompletableFuture,completableFuture1,
                completableFuture2,completableFuture3,completableFuture4).join();
        return map;
    }


}
